//
//  WYDeviceHelper.m
//  test
//
//  Created by longtu on 15/4/23.
//  Copyright (c) 2015年 longtu. All rights reserved.
//

#import "WYDeviceHelper.h"
#import "OpenUDID.h"

#import <SystemConfiguration/CaptiveNetwork.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreTelephony/CTCarrier.h>
#import <AdSupport/AdSupport.h>
#include "mach/mach.h"
#include <sys/sysctl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#import <sys/utsname.h>
#include <netinet/in.h>
#include <net/if_dl.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <arpa/inet.h>

NSString * const UIDeviceModeliPhone1G                      = @"iPhone 1G";
NSString * const UIDeviceModeliPhone3G                      = @"iPhone 3G";
NSString * const UIDeviceModeliPhone3GS                     = @"iPhone 3GS";

NSString * const UIDeviceModeliPhone4                       = @"iPhone 4";
NSString * const UIDeviceModelVerizoniPhone4                = @"Verizon iPhone 4";
NSString * const UIDeviceModeliPhone4S                      = @"iPhone 4S";

NSString * const UIDeviceModeliPhone5_GSM                   = @"iPhone 5 (GSM)";
NSString * const UIDeviceModeliPhone5_GSM_CDMA              = @"iPhone 5 (GSM+CDMA)";
NSString * const UIDeviceModeliPhone5C_GSM                  = @"iPhone 5C (GSM)";
NSString * const UIDeviceModeliPhone5C_Global               = @"iPhone 5C (Global)";

NSString * const UIDeviceModeliPhone5S_GSM                  = @"iPhone 5S (GSM)";
NSString * const UIDeviceModeliPhone5S_Global               = @"iPhone 5S (Global)";

NSString * const UIDeviceModeliPhone6                       = @"iPhone 6";
NSString * const UIDeviceModeliPhone6Plus                   = @"iPhone 6 Plus";

NSString * const UIDeviceModeliPodTouch1G                   = @"iPod Touch 1G";
NSString * const UIDeviceModeliPodTouch2G                   = @"iPod Touch 2G";
NSString * const UIDeviceModeliPodTouch3G                   = @"iPod Touch 3G";
NSString * const UIDeviceModeliPodTouch4G                   = @"iPod Touch 4G";
NSString * const UIDeviceModeliPodTouch5G                   = @"iPod Touch 5G";

NSString * const UIDeviceModeliPad                          = @"iPad";
NSString * const UIDeviceModeliPad2_Wifi                    = @"iPad 2 (WiFi)";
NSString * const UIDeviceModeliPad2_GSM                     = @"iPad 2 (GSM)";
NSString * const UIDeviceModeliPad2_CDMA                    = @"iPad 2 (CDMA)";
NSString * const UIDeviceModeliPad2                         = @"iPad 2";
NSString * const UIDeviceModeliPad3G_Wifi                   = @"iPad-3G (WiFi)";
NSString * const UIDeviceModeliPad3G_4G                     = @"iPad-3G (4G)";
NSString * const UIDeviceModeliPad4G_Wifi                   = @"iPad-4G (WiFi)";
NSString * const UIDeviceModeliPad4G_GSM                    = @"iPad-4G (GSM)";
NSString * const UIDeviceModeliPad4G_GSM_CDMA               = @"iPad-4G (GSM+CDMA)";

NSString * const UIDeviceModeliPadMini1G_Wifi               = @"iPad mini-1G (WiFi)";
NSString * const UIDeviceModeliPadMini1G_GSM                = @"iPad mini-1G (GSM)";
NSString * const UIDeviceModeliPadMini1G_GSM_CDMA           = @"iPad mini-1G (GSM+CDMA)";
NSString * const UIDeviceModeliPadMiniRetina2G_Wifi         = @"iPad mini 2G Retina (WiFi)";
NSString * const UIDeviceModeliPadMiniRetina2G_Cellular     = @"iPad mini 2G Retina (Cellular)";
NSString * const UIDeviceModeliPadAir_Wifi                  = @"iPad Air (WiFi)";
NSString * const UIDeviceModeliPadAir_Cellular              = @"iPad Air (Cellular)";
NSString * const UIDeviceModeliPadAir_4GCellular            = @"iPad Air (4G)";
NSString * const UIDeviceModeliPadMiniRetina4G_Cellular     = @"iPad mini (4G)";

NSString * const UIDeviceModeliPadAir2_Wifi                 = @"iPad Air 2 (WiFi)";
NSString * const UIDeviceModeliPadAir2_Cellular             = @"iPad Air 2 (Cellular)";
NSString * const UIDeviceModeliPadMiniRetina3_Wifi          = @"iPad mini 3 Retina (Wifi)";
NSString * const UIDeviceModeliPadMiniRetina3_Cellular      = @"iPad mini 3 Retina (Cellular)";

NSString * const UIDeviceModelSimulatorI386                 = @"iPhone Simulator (i386)";
NSString * const UIDeviceModelSimulatorX86_64               = @"iPhone Simulator (x86_64)";
NSString * const UIDeviceModelSimulator                     = @"iPhone Simulator";

@interface WYDeviceHelper () <CLLocationManagerDelegate>

@end

@implementation WYDeviceHelper
#define MB (1024*1024)
#define GB (MB*1024)

#define SS (60)
#define HH (SS*SS)

#pragma mark - UIDevice
+ (UIDevice *) OSDevice {
    [UIDevice currentDevice].batteryMonitoringEnabled = YES;
    return [UIDevice currentDevice];
}

// 手机别名： 用户定义的名称
+ (NSString *) OSUserPhoneName{
    return [[self OSDevice] name];
}

// 设备名称
+ (NSString *) OSSystemName{
    return [[self OSDevice] systemName];
}

// 设备型号
+(NSString*) OSPhoneModel{
    size_t size;
    int nR = sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = (char *)malloc(size);
    nR = sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
    free(machine);
    
    if ([platform isEqualToString:@"iPhone1,1"])    return UIDeviceModeliPhone1G;
    if ([platform isEqualToString:@"iPhone1,2"])    return UIDeviceModeliPhone3G;
    if ([platform isEqualToString:@"iPhone2,1"])    return UIDeviceModeliPhone3GS;
    if ([platform isEqualToString:@"iPhone3,1"])    return UIDeviceModeliPhone4;
    if ([platform isEqualToString:@"iPhone3,2"])    return UIDeviceModeliPhone4;
    if ([platform isEqualToString:@"iPhone3,3"])    return UIDeviceModelVerizoniPhone4;
    if ([platform isEqualToString:@"iPhone4,1"])    return UIDeviceModeliPhone4S;
    if ([platform isEqualToString:@"iPhone5,1"])    return UIDeviceModeliPhone5_GSM;
    if ([platform isEqualToString:@"iPhone5,2"])    return UIDeviceModeliPhone5_GSM_CDMA;
    if ([platform isEqualToString:@"iPhone5,3"])    return UIDeviceModeliPhone5C_GSM;
    if ([platform isEqualToString:@"iPhone5,4"])    return UIDeviceModeliPhone5C_Global;
    if ([platform isEqualToString:@"iPhone6,1"])    return UIDeviceModeliPhone5S_GSM;
    if ([platform isEqualToString:@"iPhone6,2"])    return UIDeviceModeliPhone5S_Global;
    if ([platform isEqualToString:@"iPhone7,1"])    return UIDeviceModeliPhone6Plus;
    if ([platform isEqualToString:@"iPhone7,2"])    return UIDeviceModeliPhone6;
    
    if ([platform isEqualToString:@"iPod1,1"])      return UIDeviceModeliPodTouch1G;
    if ([platform isEqualToString:@"iPod2,1"])      return UIDeviceModeliPodTouch2G;
    if ([platform isEqualToString:@"iPod3,1"])      return UIDeviceModeliPodTouch3G;
    if ([platform isEqualToString:@"iPod4,1"])      return UIDeviceModeliPodTouch4G;
    if ([platform isEqualToString:@"iPod5,1"])      return UIDeviceModeliPodTouch5G;
    
    if ([platform isEqualToString:@"iPad1,1"])      return UIDeviceModeliPad;
    if ([platform isEqualToString:@"iPad2,1"])      return UIDeviceModeliPad2_Wifi;
    if ([platform isEqualToString:@"iPad2,2"])      return UIDeviceModeliPad2_GSM;
    if ([platform isEqualToString:@"iPad2,3"])      return UIDeviceModeliPad2_CDMA;
    if ([platform isEqualToString:@"iPad2,4"])      return UIDeviceModeliPad2;
    if ([platform isEqualToString:@"iPad2,5"])      return UIDeviceModeliPadMini1G_Wifi;
    if ([platform isEqualToString:@"iPad2,6"])      return UIDeviceModeliPadMini1G_GSM;
    if ([platform isEqualToString:@"iPad2,7"])      return UIDeviceModeliPadMini1G_GSM_CDMA;
    if ([platform isEqualToString:@"iPad3,1"])      return UIDeviceModeliPad3G_Wifi;
    if ([platform isEqualToString:@"iPad3,2"])      return UIDeviceModeliPad3G_4G;
    if ([platform isEqualToString:@"iPad3,3"])      return UIDeviceModeliPad3G_4G;
    if ([platform isEqualToString:@"iPad3,4"])      return UIDeviceModeliPad4G_Wifi;
    if ([platform isEqualToString:@"iPad3,5"])      return UIDeviceModeliPad4G_GSM;
    if ([platform isEqualToString:@"iPad3,6"])      return UIDeviceModeliPad4G_GSM_CDMA;
    if ([platform isEqualToString:@"iPad4,1"])      return UIDeviceModeliPadAir_Wifi;
    if ([platform isEqualToString:@"iPad4,2"])      return UIDeviceModeliPadAir_Cellular;
    if ([platform isEqualToString:@"iPad4,3"])      return UIDeviceModeliPadAir_4GCellular;
    if ([platform isEqualToString:@"iPad4,4"])      return UIDeviceModeliPadMiniRetina2G_Wifi;
    if ([platform isEqualToString:@"iPad4,5"])      return UIDeviceModeliPadMiniRetina2G_Cellular;
    if ([platform isEqualToString:@"iPad4,6"])      return UIDeviceModeliPadMiniRetina4G_Cellular;
    
    if ([platform isEqualToString:@"iPad4,7"])      return UIDeviceModeliPadMiniRetina3_Wifi;
    if ([platform isEqualToString:@"iPad4,8"])      return UIDeviceModeliPadMiniRetina3_Cellular;
    if ([platform isEqualToString:@"iPad4,9"])      return UIDeviceModeliPadMiniRetina3_Cellular;
    if ([platform isEqualToString:@"iPad5,3"])      return UIDeviceModeliPadAir2_Wifi;
    if ([platform isEqualToString:@"iPad5,4"])      return UIDeviceModeliPadAir2_Cellular;
    
    if ([platform isEqualToString:@"i386"])         return UIDeviceModelSimulator;
    if ([platform isEqualToString:@"x86_64"])       return UIDeviceModelSimulator;
    
    return platform;
}

// 地方型号 (国际化区域名称)
+ (NSString *) OSLocalPhoneModel{
    return [[self OSDevice] localizedModel];
}

// 设备类型
+ (NSString *) OSModel{
    NSString * dev = [self OSDevice].model;
    if (TARGET_IPHONE_SIMULATOR) {
        dev = @"SIMULATOR";
    }
    return dev;
}

// 手机系统版本
+ (NSString *) OSPhoneVersion{
    return [[self OSDevice] systemVersion];
}

// UDID
+ (NSString *) deveiceUDID{
    return [OpenUDID valueWithError:nil];
}

// IDFA
+ (NSString *) deveiceADFA{
    NSString *adId = [OpenUDID valueWithError:nil];
    if ([[self OSPhoneVersion] floatValue] >= 6.0){
        // 可以获取到广告标识的版本
        adId =  [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    }
    return adId;
}

// 当前应用名称
+ (NSString *) appName{
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
}

// 当前应用软件版本
+ (NSString *) appVersion{
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}

// 当前应用版本号码   int类型
+ (NSString *) appVersionNum{
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
}

// 包名
+ (NSString *) appPackageName{
    return [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString*)kCFBundleIdentifierKey];
}

#pragma mark - 电池状态的信息
// 电池电量百分比
+ (NSString *) batteryLevel{
    [self OSDevice].batteryMonitoringEnabled = YES;
    return [NSString stringWithFormat:@"%.0f%%",([self OSDevice].batteryLevel * 100.0f)];
}
// 装置的电池状态 
+ (UIDeviceBatteryState)batteryState {
    return [self OSDevice].batteryState;
}
// 电池充电
+ (BOOL)inCharge {
    if ([self batteryState] == UIDeviceBatteryStateCharging ||
        [self batteryState] == UIDeviceBatteryStateFull) {
        return YES;
    } else {
        return NO;
    }
}

// 电池是否充满电
+ (BOOL)batteryFullCharged {
    CGFloat batteryLevel = 0.0f;
    CGFloat batteryCharge = [self OSDevice].batteryLevel;
    if (batteryCharge > 0.0f)
        batteryLevel = batteryCharge * 100;
    else
        // Unable to find battery level
        return -1;
    
    if (batteryLevel) {
        return YES;
    } else {
        return NO;
    }
}
// 设备插入到电源
+ (BOOL)devicePluggedIntoPower {
    if ([self batteryState] == UIDeviceBatteryStateUnplugged) {
        return NO;
    } else {
        return YES;
    }
}

// 获取小时，分钟和秒的开机时间
+ (NSString *)bootTime{
    NSInteger ti = (NSInteger)[[NSProcessInfo processInfo] systemUptime];
    NSInteger seconds = ti % SS;
    NSInteger minutes = (ti / SS) % SS;
    NSInteger hours = (ti / HH);
    return [NSString stringWithFormat:@"%02li:%02li:%02li", (long)hours, (long)minutes, (long)seconds];
}

// 屏幕的亮度
+ (NSString *) brightness {
    return [NSString stringWithFormat:@"%.2f%%",([[UIScreen mainScreen] brightness]*100)];
}

// 该设备是否越狱
+ (BOOL ) isJailbroken{
    FILE *f = fopen("/bin/bash", "r");
    BOOL isJailbroken = NO;
    if (f != NULL)
        // Device is jailbroken
        isJailbroken = YES;
    else
        // Device isn't jailbroken
        isJailbroken = NO;
    
    fclose(f);
    return isJailbroken;
}

#pragma mark - memory
// 装置的总内存
+ (CGFloat ) totalMemory {
    int nearest = 256;
    int totalMemory = (int )[[NSProcessInfo processInfo] physicalMemory]/MB;
    int rem = (int)totalMemory % nearest;
    int tot = 0;
    if (rem >= nearest/2) {
        tot = ((int)totalMemory - rem)+256;
    } else {
        tot = ((int)totalMemory - rem);
    }
    return tot;
}

// 占用内存空间
+ (NSString *) freeMemorySpace{
    unsigned long freeSpace = machFreeMemory();
    return [self memoryFormatter:freeSpace];
}

// app 占用总内存百分比
+ (NSInteger) freeMemorySpacePourcent{
    unsigned long freeSpace = machFreeMemory();
    unsigned long long totalMemory = [[NSProcessInfo processInfo] physicalMemory];
    CGFloat pourcent = (CGFloat)freeSpace/totalMemory;
    return (NSInteger)(100*pourcent);
}

#pragma mark - DiskSpace
// 设备总的磁盘空间
+ (NSString *)totalDiskSpace {
    long long space = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemSize] longLongValue];
    return [self memoryFormatter:space];
}

// 设备使用的磁盘空间
+ (NSString *)usedDiskSpace{
    return [self memoryFormatter:[self usedDiskSpaceInBytes]];
}
// 设备可用磁盘空间
+ (NSString *) freeDiskSpace{
    long long freeSpace = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemFreeSize] longLongValue];
    return [self memoryFormatter:freeSpace];
}

// 以字节为单位的总磁盘空间
+ (CGFloat)totalDiskSpaceInBytes {
    long long space = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemSize] longLongValue];
    return space;
}

// 以字节为单位的可用磁盘空间
+ (CGFloat)freeDiskSpaceInBytes {
    long long freeSpace = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemFreeSize] longLongValue];
    return freeSpace;
}

// 以字节为单位所使用的磁盘空间
+ (CGFloat)usedDiskSpaceInBytes {
    long long usedSpace = [self totalDiskSpaceInBytes] - [self freeDiskSpaceInBytes];
    return usedSpace;
}


#pragma mark - Private methods
#pragma mark *memory
+ (NSString *) memoryFormatter:(long long)diskSpace {
    NSString *formatted;
    double bytes = 1.0 * diskSpace;
    double megabytes = bytes / MB;
    double gigabytes = bytes / GB;
    if (gigabytes >= 1.0)
        formatted = [NSString stringWithFormat:@"%.2f GB", gigabytes];
    else if (megabytes >= 1.0)
        formatted = [NSString stringWithFormat:@"%.2f MB", megabytes];
    else
        formatted = [NSString stringWithFormat:@"%.2f bytes", bytes];
    
    return formatted;
}

vm_size_t machFreeMemory(void){
    mach_port_t host_port = mach_host_self();
    mach_msg_type_number_t host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
    vm_size_t pagesize;
    vm_statistics_data_t vm_stat;
    host_page_size(host_port, &pagesize);
    (void) host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size);
    return vm_stat.free_count * pagesize;
}

// Mac 地址
+ (NSString *) macAddress{
    int mib[6];
    size_t len;
    char *buf;
    unsigned char *ptr;
    struct if_msghdr *ifm;
    struct sockaddr_dl *sdl;
    mib[0] = CTL_NET;
    mib[1] = AF_ROUTE;
    mib[2] = 0;
    mib[3] = AF_LINK;
    mib[4] = NET_RT_IFLIST;
    if ((mib[5] = if_nametoindex("en0")) == 0) {
        printf("Error: if_nametoindex error/n");
        return NULL;
    }
    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 1/n");
        return NULL;
    }
    if ((buf = malloc(len)) == NULL) {
        printf("Could not allocate memory. error!/n");
        return NULL;
    }
    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 2");
        return NULL;
    }
    ifm = (struct if_msghdr *)buf;
    sdl = (struct sockaddr_dl *)(ifm + 1);
    ptr = (unsigned char *)LLADDR(sdl);
    NSString *outstring = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    free(buf);
    return [outstring uppercaseString];
}

// IP 地址
+ (NSArray *) getIPAdress {
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) return nil;
    NSMutableArray *ips = [NSMutableArray array];
    
    int BUFFERSIZE = 4096;
    struct ifconf ifc;
    char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;
    struct ifreq *ifr, ifrcopy;
    ifc.ifc_len = BUFFERSIZE;
    ifc.ifc_buf = buffer;
    if (ioctl(sockfd, SIOCGIFCONF, &ifc) >= 0){
        for (ptr = buffer; ptr < buffer + ifc.ifc_len; ){
            ifr = (struct ifreq *)ptr;
            int len = sizeof(struct sockaddr);
            if (ifr->ifr_addr.sa_len > len) {
                len = ifr->ifr_addr.sa_len;
            }
            ptr += sizeof(ifr->ifr_name) + len;
            if (ifr->ifr_addr.sa_family != AF_INET) continue;
            if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL) *cptr = 0;
            if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0) continue;
            memcpy(lastname, ifr->ifr_name, IFNAMSIZ);
            ifrcopy = *ifr;
            ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);
            if ((ifrcopy.ifr_flags & IFF_UP) == 0) continue;
            
            NSString *ip = [NSString stringWithFormat:@"%s", inet_ntoa(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr)];
            [ips addObject:ip];
        }
    }
    close(sockfd);
    return ips;
}
// 获取无线信道ssid
+ (NSString *) currentWifiSSID{
    NSString *ssid = @"SSID";
    NSArray *ifs = (id)CNCopySupportedInterfaces();
    for (NSString *ifnam in ifs) {
        NSDictionary *info = (id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);
        if (info[@"SSID"]) {
            ssid = info[@"SSID"];
        }
    }
    return ssid;
}

#pragma mark - 获取坐标
// 定位
+ (void) locationBlock:(LocationBlock)block errorBlock:(LocationError)error{
    WYDeviceHelper *location = [[WYDeviceHelper alloc]init];
    location.locationBlock = block;
    location.locationError = error;
    CLLocationManager *locationManager = nil;
    // 判断定位操作是否被允许
    if([CLLocationManager locationServicesEnabled]) {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = location;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        //控制定位服务更新频率。单位是“米”
        locationManager.distanceFilter = 100;
    }
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
        [locationManager requestWhenInUseAuthorization];  //调用了这句,就会弹出允许框了.
    // 开始定位
    [locationManager startUpdatingLocation];
}
// CLLocationManagerDelegate
- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    //此处locations存储了持续更新的位置坐标值，取最后一个值为最新位置，如果不想让其持续更新位置，则在此方法中获取到一个值之后让locationManager stopUpdatingLocation
    CLLocation *currentLocation = [locations lastObject];
    if (self.locationBlock){
        self.locationBlock(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
    }
}
- (void) locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error {
    self.locationError(error);
    if (error.code == kCLErrorDenied) {
        // 提示用户出错原因，可按住Option键点击 KCLErrorDenied的查看更多出错信息，可打印error.code值查找原因所在
    }
}

#pragma mark - 获取个人信息的载体
// 运营商名称
+ (NSString *)carrierName {
    CTTelephonyNetworkInfo *netinfo = [[[CTTelephonyNetworkInfo alloc] init] autorelease];
//    CTCarrier *carrier = [netinfo subscriberCellularProvider];
    return [[netinfo subscriberCellularProvider] carrierName];
}
// ISO国家代码
+ (NSString *)carrierISOCountryCode {
    CTTelephonyNetworkInfo *netinfo = [[[CTTelephonyNetworkInfo alloc] init] autorelease];
//    CTCarrier *carrier = [[netinfo subscriberCellularProvider] autorelease];
    return [[netinfo subscriberCellularProvider] isoCountryCode];
}
// 运营商移动国家代码
+ (NSString *)carrierMobileCountryCode {
    CTTelephonyNetworkInfo *netinfo = [[[CTTelephonyNetworkInfo alloc] init] autorelease];
//    CTCarrier *carrier = [netinfo subscriberCellularProvider];
    return [[netinfo subscriberCellularProvider] mobileCountryCode];
}
// 运营商移动网络代码
+ (NSString *)carrierMobileNetworkCode {
    CTTelephonyNetworkInfo *netinfo = [[[CTTelephonyNetworkInfo alloc] init] autorelease];
//    CTCarrier *carrier = [netinfo subscriberCellularProvider];
    return [[netinfo subscriberCellularProvider] mobileNetworkCode];
}
// VOIP
+ (BOOL)carrierAllowsVOIP {
    CTTelephonyNetworkInfo *netinfo = [[[CTTelephonyNetworkInfo alloc] init] autorelease];
//    CTCarrier *carrier = [netinfo subscriberCellularProvider];
    return [[netinfo subscriberCellularProvider] allowsVOIP];
}

@end
